package com.xmkj.bearuser.sort;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.bean.FirstSortBean;
import com.common.retrofit.bean.SecondSortBean;
import com.common.retrofit.bearusermethod.GetFirstSortMethods;
import com.common.retrofit.bearusermethod.GetSecondSortMethods;
import com.common.retrofit.entity.bean.SortDetailsBean;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearuser.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/26.
 */
@SuppressLint("ValidFragment")
public class ContentFragment extends BaseMvpFragment {

    public static final String TAG = "MyFragment";
    public static final String BIGTAG = "bigtag";
    public static final String CID = "cid";
    public static final String TITLE = "title";
    Context context;
    private XRecyclerView recyclerview;
    private FirstSortAdapter firstSortAdapter;
    private SecondSortAdapter secondSortAdapter;
    private List<FirstSortBean.CategoryTwoEntity> firstBean = new ArrayList<>();
    private List<SecondSortBean.CategoryTwoEntity> secondBean = new ArrayList<>();
    private int position;
    private int big;
    private SortDetailsBean sortBean;
    private String cid;

    private String title = "";
    private TextView tvTitle;

    public ContentFragment(Context context) {
        this.context = context;
    }


    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myfragment;
    }

    @Override
    protected void onViewCreated(View view) {
        isDestory = false;
        recyclerview = view.findViewById(R.id.recyclerview);
        tvTitle = view.findViewById(R.id.tv_title);
        position = getArguments().getInt(TAG);  //子分类从0开始
        title = getArguments().getString(TITLE); //大分类 从 1 开始  （1 ，0） 是个特殊点
        cid = getArguments().getInt(CID) + ""; //大分类 从 1 开始  （1 ，0） 是个特殊点
        tvTitle.setText(title);
//        if (position == 0) {         // 请求推荐类的 数据  第一级分类    goto  跟 setreclerview  都一样
//            gotoHttpFirst();
//            setFirstRecyclerView();
//        } else {   //  请求第二分类的 数据接口   goto 跟 setreclearView  也要不一样
        mPageSize = 100000;
        setRecyclerView();
        gotoHttpCid(cid);
//        }

        //得到数据
    }

    private void setFirstRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(context, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setHasFixedSize(true);
        recyclerview.setLoadMoreEnabled(true);
        recyclerview.setRefreshing(false);
//        recyclerview.setRefreshEnabled(false);
        firstSortAdapter = new FirstSortAdapter(context, firstBean);
        recyclerview.setAdapter(firstSortAdapter);

        recyclerview.setHasFixedSize(true);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;

                gotoHttpFirst();

            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;

                gotoHttpFirst();

            }
        });
    }

    private void gotoHttpFirst() {
        CommonSubscriber<FirstSortBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                FirstSortBean firstSortBean = (FirstSortBean) o;

                List<FirstSortBean.CategoryTwoEntity> beanList = firstSortBean.category_two;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    firstSortAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    firstBean = beanList;
                    firstSortAdapter.addAll(firstBean);
                    statusContent();
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                firstSortAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
                firstSortAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });

        GetFirstSortMethods.getInstance().categoryOne(subscriber);
        rxManager.add(subscriber);
    }


    private void gotoHttpCid(String cid) {
        CommonSubscriber<SecondSortBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                SecondSortBean secondSortBean = (SecondSortBean) o;

                List<SecondSortBean.CategoryTwoEntity> beanList = secondSortBean.category_two;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    secondSortAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    secondBean = beanList;
                    secondSortAdapter.addAll(secondBean);
                    statusContent();
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                secondSortAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
                secondSortAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });

        GetSecondSortMethods.getInstance().categoryTwo(subscriber, cid);
        rxManager.add(subscriber);
    }

    @Override
    protected void doLogicFunc() {

    }

    int currentnum = 1;


    private void setRecyclerView() {

        GridLayoutManager layoutManager = new GridLayoutManager(context, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setHasFixedSize(true);
        recyclerview.setLoadMoreEnabled(false);
        recyclerview.setRefreshing(false);
//        recyclerview.setRefreshEnabled(false);
        secondSortAdapter = new SecondSortAdapter(context, secondBean);
        recyclerview.setAdapter(secondSortAdapter);

        recyclerview.setHasFixedSize(true);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;

                gotoHttpCid(cid);

            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;

                gotoHttpCid(cid);

            }
        });
    }


    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }


}
